<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\Orderinfo;
use app\models\Outstock;

class OutstockController extends Controller
{
	public function actionIndex()
	{	
		$query = new \yii\db\Query();

		$displayAll = Yii::$app->request->get("displayAll",0);

		if ($displayAll == 1){
			$list = $query->select("*")->from("JXC_OUTSTOCK")->orderBy("OUTSTOCKID DESC")->all();
		}else{
			$list = $query->select("*")->from("JXC_OUTSTOCK")->orderBy("OUTSTOCKID DESC")->limit(DEFAULT_LIMIT)->all();
		}

		return $this->render("index",['list' => $list]);
	}

	private function generateQueryString($fieldName){
		if (empty($fieldName))
			return "";
		$getFieldNameRequest = $_GET[$fieldName];
		if (empty($getFieldNameRequest))
			return "";
		else
			return $fieldName." like '%".$getFieldNameRequest."%'";
	}

	public function actionSearch()
	{	
		$outtime = $_GET['outtime'];
		$outtime_end = $_GET['outtime_end'];
		$ordernum = $_GET['ordernum'];
		$orderunit = $_GET['orderunit'];
		$receivingunit = $_GET['receivingunit'];
		$drawingno = $_GET['drawingno'];

		$query = new \yii\db\Query();
		
		$query->select("*")->from("JXC_OUTSTOCK");

		$queryString = $this->generateQueryString("plannum");
		if(!empty($queryString)){
			$query->andWhere($queryString);
		}

		$queryString = $this->generateQueryString("outstocknum");
		if(!empty($queryString)){
			$query->andWhere($queryString);
		}

		if(!empty($outtime))
		{
			$query->andWhere("outtime >= '$outtime'");
		}
		if(!empty($outtime_end))
		{
			$query->andWhere("outtime <= '".$outtime_end." 23:59:59'");
		}

		if($ordernum != "" || $orderunit != "" || $receivingunit != "")
		{
			$orderidList = Orderinfo::getOrderinfoidForOutstock($ordernum,$orderunit,$receivingunit);

			if (empty($orderidList)){
				$query->andWhere("orderinfoid in (0)");
			}else{
				$query->andWhere("orderinfoid in (".$orderidList.")");
			}
		}
		
		if(!empty($drawingno))
		{
			$outstockList = Outstock::getOutstockidByDrawingno($drawingno);

			if (empty($outstockList)){
				$query->andWhere("outstockid in (0)");
			}else{
				$query->andWhere("outstockid in (".$outstockList.")");
			}
		}

		$list = $query->all();

		return $this->render("index",['list' => $list]);
	}

	public function actionItem()
	{
		$outstockid = $_GET['id'];
		$query = new\yii\db\Query();

		$orderNumber = "";
		$outstockInfo = $query->select("*")->from("JXC_OUTSTOCK")->where("OUTSTOCKID = ".$outstockid)->one();
		if (!empty($outstockInfo)){
			$orderinfoid = $outstockInfo["ORDERINFOID"];
			if (!empty($orderinfoid)){
				$orderInfo = $query->select("*")->from("JXC_ORDERINFO")->where("ORDERINFOID = ".$orderinfoid)->one();
				if(!empty($orderInfo)){
					$orderNumber = $orderInfo['ORDERNUM'];
				}
			}
		}



		$query = new \yii\db\Query();
		
		$list = $query->select("*")->from("JXC_OUTSTOCKITEM")->where('outstockid = '. $outstockid)->all();

		$sumCount = 0;
		$sumPrice = 0;
		foreach ($list as  $value) {
			$sumCount += $value['COUNT'];
			$sumPrice += $value['PRICE'];
		}

		$result = array();

		foreach($list as $value){
			$tempresult = $value;

			$inventoryid = $value["INVENTORYID"];
			$tempresult['POSITION'] = $this->getPosition($inventoryid);

			$result[] = $tempresult;
		}


		return $this->render("item",['list' => $result,'sumCount'=>$sumCount,'sumPrice'=>$sumPrice, 'orderNumber'=>$orderNumber]);
	}

	public function getPosition($inventoryid){
		$position = "";
		if (empty($inventoryid))
			return $position;
		$query = new \yii\db\Query();

		$inventoryInfo = $query->select("*")
			->from("JXC_INVENTORY")
			->where("INVENTORYID = ".$inventoryid)
			->one();

		if (empty($inventoryInfo))
			return $position;

		$position = $inventoryInfo["POSITION"];

		if (empty($position)){
			return "";
		}

		return $position;
	}
}